package org.jeecg.modules.inspect.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.inspect.entity.InsTask;
import org.jeecg.modules.inspect.entity.InsTaskItem;
import org.jeecg.modules.inspect.mapper.InsTaskItemMapper;
import org.jeecg.modules.inspect.mapper.InsTaskMapper;
import org.jeecg.modules.inspect.service.IInsTaskItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 任务项
 * @Author: jeecg-boot
 * @Date:   2023-11-24
 * @Version: V1.0
 */
@Service
public class InsTaskItemServiceImpl extends ServiceImpl<InsTaskItemMapper, InsTaskItem> implements IInsTaskItemService {

	@Resource
	private InsTaskItemMapper insTaskItemMapper;
	@Resource
	private InsTaskMapper insTaskMapper;

	@Override
	public List<InsTaskItem> selectByMainId(String mainId) {
		return insTaskItemMapper.selectByMainId(mainId);
	}

	@Override
	public void updateStatus(String mainId){
		//查询当前任务下全部任务项
		List<InsTaskItem> insTaskItems = insTaskItemMapper.selectList(Wrappers.<InsTaskItem>lambdaQuery()
				.eq(InsTaskItem::getTaskId, mainId)
				/*.eq(InsTaskItem::getItemStatus, "1")*/
		);
		if(CollectionUtils.isEmpty(insTaskItems)){
			insTaskItems = new ArrayList<>();
		}
		//已巡检
		List<InsTaskItem> inspectedList = insTaskItems.stream().filter(o -> "2".equals(o.getItemStatus())).collect(Collectors.toList());
		//未巡检
		List<InsTaskItem> notInspectedList = insTaskItems.stream().filter(o -> "1".equals(o.getItemStatus())).collect(Collectors.toList());
		InsTask insTask = new InsTask();
		insTask.setId(mainId);
		//已巡检数
		insTask.setInspectedNum(CollectionUtils.isNotEmpty(inspectedList)?inspectedList.size():0);
		if( CollectionUtils.isEmpty(notInspectedList) ){
			insTask.setTaskStatus("3");
		}else{
			insTask.setTaskStatus("2");
		}
		insTaskMapper.updateById(insTask);
	}

}
